package g11.testdao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.sql.Date;

import g11.dao.AppointmentDAO;
import g11.dao.AppointmentDAOImpl;
import g11.dao.DAOException;
import g11.dao.EmployeeDAO;
import g11.dao.EmployeeDAOImpl;
import g11.dao.PatientDAO;
import g11.dao.PatientDAOImpl;
import g11.dao.VisitationDAO;
import g11.dao.VisitationDAOImpl;
import g11.model.Appointment;
import g11.model.Employee;
import g11.model.Patient;
import g11.model.Visitation;

import org.junit.Test;

public class testVisitationDao {

	@Test
	public void testVisitationCRUD() throws DAOException {
		int visitationId = 1;
		Integer currentVersion = 1;
		Employee employee = new Employee();
		employee.setUsername("penny");
		employee.setPassword("1234");
		Byte type = 1;
		employee.setType(type);
		employee.setName("Penny Money");
		employee.setDepartment("Surgery");
		employee.setTitle("Surgery Doctor");
		EmployeeDAO employeeDao = new EmployeeDAOImpl();
		employeeDao.createEmployee(employee);
		Employee retrievedEmployee = new Employee();
		retrievedEmployee = employeeDao.getEmployeeByUsername("penny");
		
	     //create a patient 
			Patient patient = new Patient();
			patient.setUsername("penny");
			patient.setName("Penny Jen");
			patient.setPassword("1234");
			Byte type1 = 0;
			patient.setType(type1);
			patient.setPhone("519-888-8888");
			patient.setAddress("77 Weber Street");
			patient.setOhip("2222222222ka");
			patient.setSin("1111111111");
			patient.setCurrentHealth("Diabetes.");
			// to form a DateTime of joda typee
			Date date = new Date(12, 12, 12);
			// patient.setDepartment("Surgery");
			// patient.setTitle("Surgery Doctor");
			// create start
			PatientDAO patientDao = new PatientDAOImpl();
			patientDao.createPatient(patient);
			Patient retrievedpatient = new Patient();
			System.out.println("done.created a new patient, now getPatientByName");
			// create a temp patient
			retrievedpatient = patientDao.getPatientByName("Penny Jen");

		Appointment appointment = new Appointment();
		appointment.setDoctor(retrievedEmployee);
		appointment.setPatient(retrievedpatient);
		AppointmentDAO appointmentDao = new AppointmentDAOImpl();
		appointmentDao.createAppointment(appointment);
		Appointment retrievedAppointment = new Appointment();
		retrievedAppointment = appointmentDao.getPendingAppointmentByDoctor(
				retrievedEmployee).get(0);

		Visitation visitation = new Visitation();
		VisitationDAO visitationDao = new VisitationDAOImpl();

		visitationDao.createVisitationFromAppointment(retrievedAppointment);

		Visitation retrievedVisitation = new Visitation();
		retrievedVisitation = visitationDao
				.getCurrentVersionOfVisitation(retrievedAppointment
						.getAppointmentId());

		retrievedVisitation.setCurrentVersion(currentVersion);

		visitationDao.updateCurrentVersion(retrievedVisitation);

		retrievedVisitation = visitationDao
				.getCurrentVersionOfVisitation(retrievedVisitation
						.getVisitationId());

		assertNotNull(retrievedVisitation);
		assertEquals(currentVersion, retrievedVisitation.getCurrentVersion());

		currentVersion = 2;
		retrievedVisitation.setCurrentVersion(currentVersion);
		visitationDao.updateCurrentVersion(retrievedVisitation);

		retrievedVisitation = visitationDao
				.getCurrentVersionOfVisitation(retrievedVisitation
						.getVisitationId());

		assertEquals(currentVersion, retrievedVisitation.getCurrentVersion());

		visitationDao.deleteVisitation(retrievedVisitation);

		appointmentDao.deleteAppointment(retrievedAppointment);

		employeeDao.deleteEmployee(retrievedEmployee);

		assertNull(visitationDao.getCurrentVersionOfVisitation(visitationId));
	}

}
